package com.itheima.mybatis.test;

import com.itheima.mybatis.test.config.MybatisConfig;
import com.itheima.mybatis.test.dao.UserMapper;
import com.itheima.mybatis.test.pojo.User;
import com.itheima.mybatis.test.service.UserService;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.List;

/**
 * @author ：sean
 * @date ：Created in 2020/7/5
 * @description ：
 * @version: 1.0
 */
public class SpringMybatisApp {
	public static void main(String args[]) throws Exception{
		// 初始化上下文容器
		AnnotationConfigApplicationContext applicationContext
			= new AnnotationConfigApplicationContext(MybatisConfig.class);
    // 通过容器直接获取Mapper对象
    UserMapper userMapper = applicationContext.getBean(UserMapper.class);
    System.out.println(userMapper);
    System.out.println("第1次查询");
    User user = userMapper.findByUsername("admin");
    System.out.println(user.getEmail());
    System.out.println("第2次查询");
    user = userMapper.findByUsername("admin");
    System.out.println(user.getEmail());

    /*

    MapperFactoryBean<UserMapper> mapperFactoryBean = (MapperFactoryBean) applicationContext.getBean("&userMapper");
    UserMapper userMapper2 = mapperFactoryBean.getObject();
    System.out.println(userMapper2);
    user = userMapper.findByUsername("zhangsan");
    System.out.println(user.getEmail());
    */


    // 通过容器直接获取业务对象
    //UserService userService = applicationContext.getBean(UserService.class);
    //List<User> userList = userService.findUserList();
    //System.out.println("userList:"+userList.size());
    //userService.findUserByUsername("admin");
    //userService.findUserByUsername("zhangsan");


	}
}
